package com.zhigh.calcite.startproject.func;

import java.util.ArrayList;
import java.util.List;

public class ConcatName implements AggregateFunction<ConcatName.Concat, String, String, String> {
    @Override
    public ConcatName.Concat init() {
        return new Concat();
    }

    @Override
    public ConcatName.Concat add(ConcatName.Concat concat, String value, String delimiter) {
        concat.add(value);
        concat.setDelimiter(delimiter);
        return concat;
    }

    @Override
    public String result(ConcatName.Concat concat) {
        return concat.toString();
    }

    public static class Concat {
        private final List<String> elements = new ArrayList<>();
        private String delimiter = ";";

        public void add(String element) {
            elements.add(element);
        }

        public void setDelimiter(String delimiter) {
            this.delimiter = delimiter;
        }

        public String toString() {
            return String.join(delimiter, elements);
        }

    }
}
